'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class Snac0903
    Inherits Snac

    Public Sub New()
        MyBase.New(&H9, &H3)
    End Sub

    Private _MaxVisibleListSize As New TlvMaxVisibleListSize
    Public ReadOnly Property MaxVisibleListSize() As TlvMaxVisibleListSize
        Get
            Return _MaxVisibleListSize
        End Get
    End Property

    Private _MaxInvisibleListSize As New TlvMaxInvisibleListSize
    Public ReadOnly Property MaxInvisibleListSize() As TlvMaxInvisibleListSize
        Get
            Return _MaxInvisibleListSize
        End Get
    End Property

    Public Overrides Function CalculateDataSize() As Integer
        Throw New NotImplementedException
    End Function

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Throw New NotImplementedException
    End Function

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Snac.SizeFixPart

        Do While index < data.Count
            Dim desc As TlvDescriptor = TlvDescriptor.GetDescriptor(index, data)

            Select Case desc.TypeId
                Case &H1
                    _MaxVisibleListSize.Deserialize(data.GetRange(index, desc.TotalSize))
                Case &H2
                    _MaxInvisibleListSize.Deserialize(data.GetRange(index, desc.TotalSize))
            End Select

            index += desc.TotalSize
        Loop

        Me.SetTotalSize(index)
    End Sub
End Class

Public Class TlvMaxVisibleListSize
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H1)
    End Sub

    Public Overrides Function CalculateDataSize() As Integer
        Return 2
    End Function

    Private _MaxNumberOfVisibleListEntries As Integer
    Public Property MaxNumberOfVisibleListEntries() As Integer
        Get
            Return _MaxNumberOfVisibleListEntries
        End Get
        Set(ByVal value As Integer)
            _MaxNumberOfVisibleListEntries = value
        End Set
    End Property

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        _MaxNumberOfVisibleListEntries = ByteConverter.ToUInt16(data.GetRange(index, 2))
        index += 2
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(CUShort(_MaxNumberOfVisibleListEntries)))

        Return data
    End Function
End Class

Public Class TlvMaxInvisibleListSize
    Inherits Tlv

    Public Sub New()
        MyBase.New(&H1)
    End Sub

    Public Overrides Function CalculateDataSize() As Integer
        Return 2
    End Function

    Private _MaxNumberOfInvisibleListEntries As Integer
    Public Property MaxNumberOfInvisibleListEntries() As Integer
        Get
            Return _MaxNumberOfInvisibleListEntries
        End Get
        Set(ByVal value As Integer)
            _MaxNumberOfInvisibleListEntries = value
        End Set
    End Property

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        _MaxNumberOfInvisibleListEntries = ByteConverter.ToUInt16(data.GetRange(index, 2))
        index += 2
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(CUShort(_MaxNumberOfInvisibleListEntries)))

        Return data
    End Function
End Class